package com.java.jwt.interceptor.utils.config;

import com.java.jwt.interceptor.utils.interceptor.UserInjectionHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 用户信息注入拦截器配置
 *
 * @author Wu Qingzhuang
 * @date 2021/06/15 9:51
 */
@Slf4j
public class UserResolverConfig implements WebMvcConfigurer {

    /**
     * 添加拦截器
     *
     * @author Wu Qingzhuang
     * @date 2021/06/15 9:52
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] commonPathPatterns = getExcludeCommonPathPatterns();
        registry.addInterceptor(getHandlerInterceptor())
                .addPathPatterns("/**").order(10)
                .excludePathPatterns(commonPathPatterns);
    }

    private HandlerInterceptor getHandlerInterceptor() {
        return new UserInjectionHandlerInterceptor();
    }

    /**
     * 不注入用户信息的路径（不携带token访问的路径）
     *
     * @return java.lang.String[]
     * @author Wu Qingzhuang
     * @date 2021/06/15 9:52
     */
    private String[] getExcludeCommonPathPatterns() {
        return new String[]{
                "/user/auth/**", "/auth/**", "/api/**", "/sms/**",
                "/error", "/v2/api-docs", "/v2/api-docs-ext", "/swagger-resources/**", "/webjars/**", "/", "/csrf", "/META-INF/resources/**", "/resources/**",
                "/static/**", "/public/**", "classpath:/META-INF/resources/**", "classpath:/resources/**", "classpath:/static/**", "classpath:/public/**", "/cache/**", "/swagger-ui.html**", "/doc.html**"};
    }
}
